package com.ruoyi.web.controller.example;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.example.service.IStudentService;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.domain.Student;
import com.ruoyi.resturant.service.IResShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@Api(tags="学生信息管理")
@RestController
@RequestMapping("/student")
public class StudentController  extends BaseController {

    @Autowired
    private IStudentService studentService;
    @Autowired
    private RedisCache redisCache;
    @ApiOperation("取得所有的学生列表")
    @GetMapping("/list")
    public AjaxResult getStudentsList() {
        List<Student> rs = studentService.getStudents();

            return AjaxResult.success(rs);
        }
    /**
     * 获取学生详细信息
     */
    @ApiOperation(value = "获取学生详细信息", notes = "TODO：取得某个学生的详细信息")
    @ApiImplicitParam(name = "id", value = "学生ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Student student = studentService.selectStudentById(id);
        if(student == null) {
            throw new BizException(404, "学生id(" + id + ")不存在");
        }
        String str = JSON.toJSONString(student);
        redisCache.setCacheObject(String.valueOf(id),str,1, TimeUnit.HOURS);
        return AjaxResult.success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生
     */
    @Log(title = "学生管理",businessType = BusinessType.INSERT)
    @ApiOperation("新增学生")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Student student) {
        return AjaxResult.success(studentService.insertStudent(student));
    }

    /**
     * 修改学生
     */
    @Log(title = "学生管理",businessType = BusinessType.UPDATE)
    @ApiOperation("修改学生")
    @PutMapping
    public AjaxResult edit(@RequestBody Student student) {
        // 1 先上db里查询这个店铺存不存在，不存在的话，我就抛出异常
        Student studentDb = studentService.selectStudentById(student.getId());

        // 2 如果店铺对象在db中不存在的话，抛出异常
        if(studentDb == null) {
            throw new BizException(404, "学生id(" + student.getId() + ")不存在");
        }
        return AjaxResult.success(studentService.updateStudent(student));
    }

    /**
     * 删除学生
     */
    @Log(title = "学生管理",businessType = BusinessType.DELETE)
    @ApiOperation("删除学生")
    @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(studentService.deleteStudent(id));
    }
    }


